package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.file.transfer.core.FileInfo;
import com.stark.file.transfer.core.TfContactInfo;
import com.stark.file.transfer.core.Transferable;
import com.stark.file.transfer.core.TransferableSendManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.TransferTabAdapter;
import flc.ast.bean.SelectFileBean;
import flc.ast.bean.TransferTabBean;
import flc.ast.databinding.ActivityFileTransferBinding;
import flc.ast.dialog.FileSendShowQrDialog;
import flc.ast.fragment.AppFragment;
import flc.ast.fragment.AudioFragment;
import flc.ast.fragment.ContactsFragment;
import flc.ast.fragment.PictureFragment;
import flc.ast.fragment.VideoFragment;
import flc.ast.utils.SendFileManager;
import stark.common.basic.bean.ContactInfo;
import stark.common.basic.constant.FileType;
import stark.common.basic.event.EventStatProxy;

public class FileTransferActivity extends BaseAc<ActivityFileTransferBinding> {

    private List<Fragment> mFragmentList;
    private TransferTabAdapter mTabAdapter;
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.activity_file_transfer;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivFileTransferBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvSend.setOnClickListener(this);

        mFragmentList = new ArrayList<>();
        mFragmentList.add(new PictureFragment());
        mFragmentList.add(new VideoFragment());
        mFragmentList.add(new AudioFragment());
        mFragmentList.add(new AppFragment());
        mFragmentList.add(new ContactsFragment());
        FragmentUtils.add(getSupportFragmentManager(), mFragmentList, R.id.flFragment, 0);

        mDataBinding.rvTransferFileTab.setLayoutManager(new GridLayoutManager(mContext, 5));
        mTabAdapter = new TransferTabAdapter();
        mDataBinding.rvTransferFileTab.setAdapter(mTabAdapter);
        mTabAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSend:
                if (SendFileManager.getInstance().getSelectFileBeans().size() == 0) {
                    ToastUtils.showShort(R.string.no_choose_file_transfer_tips);
                } else {
                    List<Transferable> transferableList = new ArrayList<>();
                    List<ContactInfo> contactInfoList = new ArrayList<>();
                    for (SelectFileBean fileBean : SendFileManager.getInstance().getSelectFileBeans()) {
                        switch (fileBean.getType()) {
                            case 1://图片
                                FileInfo picFile = new FileInfo();
                                picFile.setFileName(fileBean.getName());
                                picFile.setUriStr(fileBean.getPath());
                                picFile.setFileType(FileType.IMAGE);
                                picFile.setSize(fileBean.getSize());
                                transferableList.add(picFile);
                                break;
                            case 2://视频
                                FileInfo videoFile = new FileInfo();
                                videoFile.setFileName(fileBean.getName());
                                videoFile.setUriStr(fileBean.getPath());
                                videoFile.setFileType(FileType.VIDEO);
                                videoFile.setSize(fileBean.getSize());
                                transferableList.add(videoFile);
                                break;
                            case 3://音频
                                FileInfo audioFile = new FileInfo();
                                audioFile.setFileName(fileBean.getName());
                                audioFile.setUriStr(fileBean.getPath());
                                audioFile.setFileType(FileType.AUDIO);
                                audioFile.setSize(fileBean.getSize());
                                transferableList.add(audioFile);
                                break;
                            case 4://应用
                                FileInfo appFile = new FileInfo();
                                appFile.setFileName(fileBean.getName());
                                appFile.setUriStr(fileBean.getPath());
                                appFile.setFileType(FileType.APK);
                                appFile.setSize(fileBean.getSize());
                                transferableList.add(appFile);
                                break;
                            case 5://联系人
                                ContactInfo contactInfo = new ContactInfo();
                                contactInfo.setName(fileBean.getName());
                                contactInfo.setPhone(fileBean.getPath());
                                contactInfoList.add(contactInfo);
                                TfContactInfo tfContactInfo = new TfContactInfo(contactInfoList);
                                transferableList.add(tfContactInfo);
                                break;
                            default:
                                break;
                        }
                    }
                    TransferableSendManager.getInstance().setTransferables(transferableList);
                    FileSendShowQrDialog dialog = new FileSendShowQrDialog(mContext);
                    dialog.show();
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        mTabAdapter.getItem(oldPosition).setSelected(false);
        mTabAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        mTabAdapter.getItem(position).setSelected(true);
        mTabAdapter.notifyItemChanged(position);
        FragmentUtils.showHide(position, mFragmentList);
    }

    @Override
    protected void initData() {
        mDataBinding.tvSend.setText(getString(R.string.send_num, 0));

        String[] title = getResources().getStringArray(R.array.transfer_tab_title);
        List<TransferTabBean> listTab = new ArrayList<>();
        listTab.add(new TransferTabBean(title[0], true));
        listTab.add(new TransferTabBean(title[1], false));
        listTab.add(new TransferTabBean(title[2], false));
        listTab.add(new TransferTabBean(title[3], false));
        listTab.add(new TransferTabBean(title[4], false));
        mTabAdapter.setList(listTab);
    }

    public void refreshDataCount(SendFileManager sendFileManager) {
        List<SelectFileBean> selectFileBeans = sendFileManager.getSelectFileBeans();
        if (CollectionUtils.isEmpty(selectFileBeans)) {
            mDataBinding.tvSend.setText(getString(R.string.send_num, 0));
        } else {
            mDataBinding.tvSend.setText(getString(R.string.send_num, selectFileBeans.size()));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!SendFileManager.getInstance().getSelectFileBeans().isEmpty()) {
            SendFileManager.getInstance().clearSelectFile();
        }
    }
}